//
//  BIDPickerMenuView.m
//  zhangqitong
//
//  Created by mal on 15/12/1.
//  Copyright © 2015年 mal. All rights reserved.
//

#import "BIDPickerMenuView.h"
#import "UIView+FindFirstResponder.h"

@interface BIDPickerMenuView()<UIPickerViewDataSource, UIPickerViewDelegate>
{
    UIPickerView *_myPicker;
    UIToolbar *_myToolbar;
    BOOL _bShow;
    NSArray *_dataSourceArr;
}
@end
@implementation BIDPickerMenuView
@synthesize delegate;
@synthesize bIsInputView;

- (id)initWithMenuArr:(NSArray *)arr
{
    self = [super init];
    if(self)
    {
        _dataSourceArr = [[NSArray alloc] initWithArray:arr];
        [self initView];
    }
    return self;
}

- (void)initView
{
    self.autoresizesSubviews = NO;
    _bShow = NO;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect ownFrame = self.frame;
    ownFrame.size.width = screenSize.width;
    ownFrame.size.height = 260.0f;
    ownFrame.origin.x = 0;
    ownFrame.origin.y = screenSize.height;
    self.frame = ownFrame;
    
    //myToolBar
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelBtnHandler)];
    item1.tintColor = [UIColor whiteColor];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(confirmBtnHandler)];
    item3.tintColor = [UIColor whiteColor];
    _myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, ownFrame.size.width, 44.0f)];
    [_myToolbar setItems:[[NSArray alloc] initWithObjects:item1, item2, item3, nil]];
    _myToolbar.barStyle = UIBarStyleBlack;
    _myToolbar.translucent = YES;
    [self addSubview:_myToolbar];
    
    //pickerView
    CGSize pickerSize = CGSizeMake(ownFrame.size.width, 216.0f);
    _myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.f, 44.f, pickerSize.width, pickerSize.height)];
    [_myPicker setBackgroundColor:[UIColor whiteColor]];
    _myPicker.dataSource = self;
    _myPicker.delegate = self;
    _myPicker.showsSelectionIndicator = YES;
    [_myPicker selectRow:0 inComponent:0 animated:NO];
    [self addSubview:_myPicker];
}

- (void)updateDataSourceWithArr:(NSArray *)arr
{
    _dataSourceArr = [[NSArray alloc] initWithArray:arr];
}

- (void)confirmBtnHandler
{
    _bShow = NO;
    __block CGRect frame1 = self.frame;
    NSInteger row = [_myPicker selectedRowInComponent:0];
    NSString *strMenu = _dataSourceArr[row];
    if(bIsInputView)
    {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        UITextField *tf = (UITextField*)[keyWindow findFirstResponder];
        [tf resignFirstResponder];
        if([delegate respondsToSelector:@selector(chooseMenu:)])
        {
            [delegate chooseMenu:strMenu];
        }
        if([delegate respondsToSelector:@selector(chooseMenuAtIndex:)])
        {
            [delegate chooseMenuAtIndex:row];
        }
    }
    else
    {
        [UIView animateWithDuration:0.35f animations:^{frame1.origin.y=[UIScreen mainScreen].bounds.size.height;self.frame = frame1;} completion:^(BOOL finished){
            if([delegate respondsToSelector:@selector(chooseMenu:)])
            {
                [delegate chooseMenu:strMenu];
            }
            if([delegate respondsToSelector:@selector(chooseMenuAtIndex:)])
            {
                [delegate chooseMenuAtIndex:row];
            }
            [self removeFromSuperview];
        }];
    }
}

- (void)cancelBtnHandler
{
    if(bIsInputView)
    {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        UITextField *tf = (UITextField*)[keyWindow findFirstResponder];
        [tf resignFirstResponder];
    }
    else
    {
        [self dismissView];
    }
}

- (void)showView
{
    if(!_bShow)
    {
        _bShow = YES;
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        
        __block CGRect frame1 = self.frame;
        [keyWindow addSubview:self];
        [UIView animateWithDuration:0.6f animations:^{frame1.origin.y = [UIScreen mainScreen].bounds.size.height-246; self.frame = frame1;}];
    }
}

- (void)dismissView
{
    _bShow = NO;
    __block CGRect frame1 = self.frame;
    [UIView animateWithDuration:0.35f animations:^{frame1.origin.y=[UIScreen mainScreen].bounds.size.height;self.frame = frame1;} completion:^(BOOL finished){[self removeFromSuperview];}];
}

#pragma mark UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _dataSourceArr.count;
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _dataSourceArr[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 320.0f;
}

@end
